#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 

class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        vector<double> ret;
        if (root == nullptr)
        {
            return ret;
        }
        ret.push_back(root->val);
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        while (!q.empty())
        {
            int n = q.size();
            int p1 = 0;
            double num = 0;
            while (n > 0)
            {
                TreeNode* left = q.front();
                q.pop();
                TreeNode* right = q.front();
                q.pop();
                if (left)
                {
                    num += left->val;
                    q.push(left->left);
                    q.push(left->right);
                    p1++;
                }
                if (right)
                {
                    num += right->val;
                    q.push(right->left);
                    q.push(right->right);
                    p1++;
                }
                n -= 2;
            }
            num /= p1;
            ret.push_back(num);
        }
        ret.pop_back();
        return ret;
    }
};

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if (root == nullptr)
        {
            return ret;
        }
        vector<int> v1;
        v1.push_back(root->val);
        ret.push_back(v1);
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        while (!q.empty())
        {
            int n = q.size();
            vector<int> v;
            while (n > 0)
            {
                TreeNode* left = q.front();
                q.pop();
                TreeNode* right = q.front();
                q.pop();
                if (left)
                {
                    v.push_back(left->val);
                    q.push(left->left);
                    q.push(left->right);
                }
                if (right)
                {
                    v.push_back(right->val);
                    q.push(right->left);
                    q.push(right->right);
                }
                n -= 2;
            }
            if (v.size())
            {
                ret.push_back(v);
            }
        }
        return ret;
    }
};

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if (!root)
        {
            return ans;
        }
        queue<TreeNode*> nodeQueue;
        nodeQueue.push(root);
        bool isOrderLeft = true;
        while (!nodeQueue.empty()) {
            deque<int> levelList;
            int size = nodeQueue.size();
            for (int i = 0; i < size; ++i) {
                auto node = nodeQueue.front();
                nodeQueue.pop();
                if (isOrderLeft)
                {
                    levelList.push_back(node->val);
                }
                else
                {
                    levelList.push_front(node->val);
                }
                if (node->left)
                {
                    nodeQueue.push(node->left);
                }
                if (node->right)
                {
                    nodeQueue.push(node->right);
                }
            }
            ans.emplace_back(vector<int>{levelList.begin(), levelList.end()});
            isOrderLeft = !isOrderLeft;
        }
        return ans;
    }
};

int main()
{
    return 0;
}